﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace FleetServerManager
{
	public class SocketHelper
	{
		private TcpClient _client;
		private string _incomingMessage;
		private string _outgoingResponse;

		byte[] bytesSent;

		public void ProcessMessage(TcpClient client, NetworkStream stream, byte[] bytesRecieved)
		{
			// Handle the message we recieved.
			_incomingMessage = Encoding.ASCII.GetString(bytesRecieved, 0, bytesRecieved.Length);
			_client = client;
			// Clean up message.
			_incomingMessage = _incomingMessage.Trim().Replace("\0", string.Empty);

			Console.WriteLine("Message Recieved: " + _incomingMessage);

			if(_incomingMessage.Equals("Hello"))
			{
				_outgoingResponse = "Good Bye!";
			}
			else{
				_outgoingResponse = "What?";
			}

			bytesSent = Encoding.ASCII.GetBytes(_outgoingResponse);
			stream.Write(bytesSent, 0, bytesSent.Length);
			Console.WriteLine("Message Responded: " + _outgoingResponse);
		}
	}
}
